import {login, register, getInfo} from '@/api/user'

const getDefaultState = () => {
    return {
        nickName: '',
        gender: '',
        phoneNumber: '',
        email: '',
        avatar: '',
        id: 0,
        username: '',
        type:''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_NAME: (state, name) => {
        state.nickName = name
    },
    SET_GENDER: (state, gender) => {
        state.gender = gender
    },
    SET_PHONE: (state, phoneNumber) => {
        state.phoneNumber = phoneNumber
    },
    SET_EMAIL: (state, email) => {
        state.email = email
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ID: (state, id) => {
        state.id = id
    },
    SET_USERNAME: (state, username) => {
        state.username = username
    },
    SET_TYPE: (state, type) => {
        state.type = type
    }
}

const actions = {
    // user login
    login({commit}, userInfo) {
        const {username, password} = userInfo
        return new Promise((resolve, reject) => {
            login({username: username.trim(), password: password}).then(response => {
                const {data} = response
                commit('SET_ID', data.id)
                sessionStorage.setItem('id',data.id)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    
    register({commit}, userInfo) {
        const {username, password, type} = userInfo
        return new Promise((resolve, reject) => {
            register({username, password, type}).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    // get user info
    getInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            getInfo(sessionStorage.getItem('id')).then(response => {
                const {data} = response
                const avatar = (data.avatar === '' || data.avatar == null) ? require('@/assets/avatar.png') : process.env.VUE_APP_BASE_API + data.avatar
                commit('SET_AVATAR', avatar)
                const {nickName, gender, phoneNumber, email, username, id, type} = data
                commit('SET_GENDER', gender)
                commit('SET_PHONE', phoneNumber)
                commit('SET_EMAIL', email)
                commit('SET_NAME', nickName)
                commit('SET_USERNAME', username)
                commit('SET_ID', id)
                commit('SET_TYPE', type)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    
    // user logout
    logout({commit, state}) {
        return new Promise((resolve, reject) => {
            sessionStorage.removeItem('id')
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

